package com.example.navigationdemo2

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.example.navigationdemo2.databinding.FragmentHomeBinding
import kotlinx.android.synthetic.main.fragment_home.*

/**
 * A simple [Fragment] subclass.
 */
class HomeFragment: Fragment(), OnSeekBarChangeListener {

    private var mViewModel: MyViewModel?=null


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        mViewModel=ViewModelProvider(activity!!).get(MyViewModel::class.java)
        //注意=======================.inflater
        val homeBinding:FragmentHomeBinding= DataBindingUtil.inflate(inflater,R.layout.fragment_home,container,false)
        //设置数据源
        homeBinding.data=mViewModel
        //设置生命周期
        homeBinding.lifecycleOwner=activity
        return homeBinding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        seekBar.progress=mViewModel!!.getIndex()!!.value as Int
        seekBar.setOnSeekBarChangeListener(this)
        //点击事件
        button.setOnClickListener(View.OnClickListener {
            Navigation.findNavController(it)
                .navigate(R.id.action_homeFragment_to_detailFragment)
        })

    }


    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
        mViewModel!!.getIndex().value=progress
    }

    override fun onStartTrackingTouch(seekBar: SeekBar?) {

    }

    override fun onStopTrackingTouch(seekBar: SeekBar?) {

    }


}
